<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 按关键字搜索商品 -->
      <el-row>
        <el-input
          class="queryinput"
          placeholder="请输入内容"
          v-model="queryinfo.query"
          clearable
          @clear="getGoodList()"
        >
          <el-button slot="append" icon="el-icon-search" @click="getGoodList()"></el-button>
        </el-input>
        <el-button type="primary" @click="addgoodPage">添加商品</el-button>
      </el-row>
      <!-- 商品渲染表格 -->
      <el-table :data="goodsList" border style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_number" label="商品数量" width="100"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="120"></el-table-column>
        <el-table-column label="修改时间" width="180">
          <template slot-scope="scope">
             {{scope.row.upd_time | getDate}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGood(scope.row.goods_id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleSizeChange"
        :current-page.sync="queryinfo.pagenum"
        :page-sizes="[10, 20, 30]"
        :page-size.sync="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0,
    };
  },
  methods: {
    async getGoodList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryinfo
      });
      // res.data.goods.forEach(item => {
      //     item.add_time=new Date(item.add_time);
      //     item.upd_time=new Date(item.upd_time)+'';
      // });

      
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品列表失败");
      }
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    // 点击分页器是重新获取当前商品数据
    handleSizeChange() {
      this.getGoodList();
    },
    // 前往增加商品页面
    addgoodPage(){
      this.$router.push('/goods/add')
      

    },
    // 增加新商品
    addGood() {},
    // 删除商品
    deleteGood(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`goods/${row}`);
          if (res.meta.status !== 200) {
            return this.$message.error("删除商品失败");
          }
          this.$message({
            type: "success",
            message: "删除商品成功!"
          });
          this.getGoodList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  created() {
    this.getGoodList();
  }
};
</script>
<style lang="less" scoped>
.queryinput {
  width: 400px;
  margin-right: 20px;
}
</style>